from flask import Flask, jsonify, request
from flask_cors import *  # 导入模块

app = Flask(__name__)
img1 = [{
            'url': 'https://t7.baidu.com/it/u=378254553,3884800361&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=9f9193799de3f3c6d7fa0a34ace29c36',
            'width': 189, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=372ac8f6f49f0fc1294b82457e5ba0c9',
            'width': 279, 'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=38a41c490151647f43b12de15dd7aedc',
            'width': 200, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=6e2efa413819839b3351ca56117ed591',
            'width': 450, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=2247852322,986532796&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=79532effb601c1f9b0360772f9f58444',
            'width': 450, 'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=378254553,3884800361&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=9f9193799de3f3c6d7fa0a34ace29c36',
            'width': 189, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=372ac8f6f49f0fc1294b82457e5ba0c9',
            'width': 279, 'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=38a41c490151647f43b12de15dd7aedc',
            'width': 200, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=6e2efa413819839b3351ca56117ed591',
            'width': 450, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=2247852322,986532796&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=79532effb601c1f9b0360772f9f58444',
            'width': 450, 'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=378254553,3884800361&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=9f9193799de3f3c6d7fa0a34ace29c36',
            'width': 189, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=372ac8f6f49f0fc1294b82457e5ba0c9',
            'width': 279, 'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=38a41c490151647f43b12de15dd7aedc',
            'width': 200, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=6e2efa413819839b3351ca56117ed591',
            'width': 450, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=2247852322,986532796&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=79532effb601c1f9b0360772f9f58444',
            'width': 450, 'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=378254553,3884800361&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=9f9193799de3f3c6d7fa0a34ace29c36',
            'width': 189, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=372ac8f6f49f0fc1294b82457e5ba0c9',
            'width': 279, 'height': 300}, {
            'url': 'https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=38a41c490151647f43b12de15dd7aedc',
            'width': 200, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=6e2efa413819839b3351ca56117ed591',
            'width': 450, 'height': 300}, {
            'url': 'https://t8.baidu.com/it/u=2247852322,986532796&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1601006244&t=79532effb601c1f9b0360772f9f58444',
            'width': 450, 'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300}]
img2 = [{'url': 'https://t9.baidu.com/it/u=2502882521,321079790&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1663565633,1824408680&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 204,
         'height': 299},
        {'url': 'https://t7.baidu.com/it/u=3228836643,3370352928&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=1505174706,1304149937&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3490059827,1745863269&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=353721707,184451439&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=2131955910,2451011674&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 223,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1775900009,2371222529&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 223,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3228836643,3370352928&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=1505174706,1304149937&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3490059827,1745863269&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=353721707,184451439&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=2131955910,2451011674&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 223,
         'height': 300}, {
            'url': 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600529045856&di=a2d7f010f266f559b7d9aaed8dcd579d&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D3909025841%2C231380458%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D970',
            'width': 1280, 'height': 970}, {
            'url': 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600529045855&di=e4f511c0d7e0655b50c9c153508993e7&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D2525299258%2C3199534338%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D1849',
            'width': 1280, 'height': 1849}, {
            'url': 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600529045852&di=8baad2f3bc962905ab99ba23f77ef834&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D959058116%2C3736322135%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D2012',
            'width': 1280, 'height': 2012},
        {'url': 'https://t9.baidu.com/it/u=3490059827,1745863269&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=353721707,184451439&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=2131955910,2451011674&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 223,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1775900009,2371222529&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 223,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3204887199,3790688592&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 466,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 463,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 449,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 533,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=2268908537,2815455140&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 534,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=3228836643,3370352928&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t7.baidu.com/it/u=1505174706,1304149937&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300},
        {'url': 'https://t9.baidu.com/it/u=3490059827,1745863269&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg', 'width': 450,
         'height': 300}]
CORS(app, supports_credentials=True)  # 设置跨域


@app.route('/search', )
def img():
    page = int(request.args.get('page', None))
    data = {
        "images": img1,
        "total": 4
    }
    if page == 1:
        data = data
    if page == 2:
        data = {
            "images": img2,
            "total": 4
        }
    if page > 4:
        return jsonify({"images": [], 'total': 4})
    return jsonify(data)
    # return jsonify({'error': '参数错误'})


import requests
from PIL import Image
from io import BytesIO, StringIO


def get_img_size():
    arr = []
    for i in img2:
        url = i
        response = requests.get(url)
        f = BytesIO(response.content)
        img = Image.open(f)
        w, h = img.size
        arr.append({
            'url': i,
            'width': w,
            'height': h
        })


if __name__ == '__main__':
    app.run(port=5000, host='0.0.0.0', debug=True)
    # get_img_size()
